package com.maomao.excel;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;

/**
 * Created by admin on 2018/3/13.
 */
public class PoiCellRangeAddress {
    public static void main(String[] args) {

        //新建excel报表
        XSSFWorkbook excel = new XSSFWorkbook();
        //添加一个sheet，名字叫"我的POI之旅"
        XSSFSheet hssfSheet = excel.createSheet("我的POI之旅");

        //单元格范围 参数（int firstRow, int lastRow, int firstCol, int lastCol)
        CellRangeAddress cellRangeAddress =new CellRangeAddress(0, 3, 3, 9);

        //在sheet里增加合并单元格
        hssfSheet.addMergedRegion(cellRangeAddress);
        //生成第一行
        XSSFRow row = hssfSheet.createRow(0);
        XSSFCell first = row.createCell(3);
        first.setCellValue("合并单元格");
        //cell 位置3-9被合并成一个单元格，
        //不管你怎样创建第4个cell还是第5个cell…然后在写数据，都是无法写入的。
        XSSFRow secondRow = hssfSheet.createRow(5);
        XSSFCell secondCell = secondRow.createCell(5);

        secondCell.setCellValue("不在合并单元格范围");

        FileOutputStream fout = null;
        try{
            fout = new FileOutputStream("F:/useFile/students.xlsx");
            excel.write(fout);
            fout.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }


}
